<template>
  <div class="app-container">
    <!-- 条件栏 -->
    <PageFilter v-model:query="filterInfo.query" :filter-list="filterInfo.list" />
    <!-- 表格 -->
    <TableContainer
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="getThirdSystem"
      v-model:query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :check-box="false"
      :tab-index="true"
    ></TableContainer>
    <!-- 新增修改弹窗 -->
    <PageDialog
      v-if="dialogInfo.visible"
      :title="dialogInfo.title[dialogInfo.type]"
      v-model:visible="dialogInfo.visible"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <!-- form -->
      <PageForm
        class="paddingBox order-model-formBox flex_wrap_start_start"
        v-model:ref-obj="formInfo.ref"
        :data="formInfo.data"
        :field-list="formInfo.fieldList"
        :rules="formInfo.rules"
        :label-width="formInfo.labelWidth"
        :list-type-info="listTypeInfo"
      />
    </PageDialog>
  </div>
</template>

<script setup name="thirdSystemLogin">
import {
  getThirdSystem,
  createThirdSystem,
  putThirdSystem,
  deleteThirdSystem,
  startThirdSystem,
  stopThirdSystem,
  thirdSystemLoginTestConnection
} from '@/api/base.js'

const { proxy } = getCurrentInstance()
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'create_time desc',
    searchType: 'orderSequence',
  },
  list: [
    {
      type: 'button',
      label: '新增',
      show: proxy.$permission('thirdSystemLogin:add'),
      event: create,
    },
    // {
    //   type: "input",
    //   label: "名称/编码",
    //   value: "keyword",
    //   event: search
    // },
    // { type: "button", label: "搜索", event: search }
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '客户端ID', value: 'clientId' },
    { label: '客户端名称', value: 'name' },
    { label: '接口url', value: 'url' },
    { label: '认证地址', value: 'tokenUrl' },
    { label: '用户名', value: 'username' },
    { label: '密码', value: 'password' },
    { label: '启停用状态', value: 'isEnableValue' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '修改',
        event: update,
        show: proxy.$permission('thirdSystemLogin:update'),
        ifRender: function (row) {
          return row.isEnable === 0
        },
      },
      // {
      //   label: "删除",
      //   event: del,
      //   show: proxy.$permission("thirdSystemLogin:del"),
      //   ifRender: function(row) {
      //     return row.isEnable === 0;
      //   }
      // },
      {
        label: '启用',
        event: enable,
        show: proxy.$permission('thirdSystemLogin:enable'),
        ifRender: function (row) {
          return row.isEnable === 0
        },
      },
      {
        label: '停用',
        event: disable,
        show: proxy.$permission('thirdSystemLogin:disable'),
        ifRender: function (row) {
          return row.isEnable === 1
        },
      },
    ],
  },
})
// 表单相关
const formInfo = ref({
  ref: null,
  data: {},
  fieldList: [
    {
      label: '客户端ID',
      value: 'clientId',
      type: 'select-model',
      code: 'client_id',
      required: true,
    },
    { label: '客户端名称', value: 'name', type: 'input', required: true },
    {
      label: '接口url',
      value: 'url',
      type: 'input',
      required: true,
      maxlength: 200,
    },
    {
      label: '认证地址',
      value: 'tokenUrl',
      type: 'input',
      maxlength: 200,
      required: true,
    },
    { label: '用户名', value: 'username', type: 'input' },
    { label: '密码', value: 'password', type: 'password' },
    {
      label: '是否启用',
      value: 'isEnable',
      type: 'radio-model',
      code: 'yes_or_no',
      required: true,
    },
  ],
  rules: {},
  labelWidth: '100px',
})
const listTypeInfo = ref({
  enableList: [
    {
      key: '是',
      value: 1,
    },
    {
      key: '否',
      value: 0,
    },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: {
    create: '添加',
    update: '修改',
  },
  visible: false,
  type: '',
  width: '40%',
  btLoading: false,
  btList: [
    { label: '测试连接', type: '', icon: '', event: testConnection, show: proxy.$permission('thirdSystemLogin:testConnection') },
    { label: '关闭', type: '', icon: '', event: close, show: true },
    {
      label: '保存',
      type: 'primary',
      icon: '',
      event: save,
      saveLoading: false,
      show: true,
    },
  ],
})
const selectUserInfo = ref({
  visible: false,
  saveEmpty: true,
  query: {
    type: 'normal',
  },
})
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
// 初始化验证
function initRules() {
  formInfo.value.rules = proxy.$initRules(formInfo.value.fieldList)
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 创建
function create() {
  formInfo.value.data = {}
  dialogInfo.value.type = 'create'
  dialogInfo.value.visible = true
}
// 编辑
function update(data) {
  dialogInfo.value.type = 'update'
  dialogInfo.value.visible = true
  // 显示信息
  formInfo.value.data = { ...data }
}
// 启用
function enable(data) {
  startThirdSystem([data.id]).then((res) => {
    if (res && res.success) {
      tableInfo.value.refresh = Math.random()
    }
  })
}
// 停用
function disable(data) {
  stopThirdSystem([data.id]).then((res) => {
    if (res && res.success) {
      tableInfo.value.refresh = Math.random()
    }
  })
}
// 删除
function del(data) {
  deleteThirdSystem([data.id]).then((res) => {
    if (res && res.success) {
      tableInfo.value.refresh = Math.random()
    }
  })
}
function close() {
  dialogInfo.value.visible = false
  formInfo.value.data = {}
}
function save() {
  formInfo.value.ref.validate((valid) => {
    if (valid) {
      const params = formInfo.value.data
      const type = dialogInfo.value.type
      let api =
        (type === 'create' && createThirdSystem) ||
        (type === 'update' && putThirdSystem)
      dialogInfo.value.btLoading = true
      api(params)
        .then((res) => {
          if (res.success) {
            dialogInfo.value.visible = false
            tableInfo.value.refresh = Math.random()
          }
          dialogInfo.value.btLoading = false
        })
        .catch(() => {
          dialogInfo.value.btLoading = false
        })
    }
  })
}
function testConnection(){
  formInfo.value.ref.validate((valid) => {
    if (valid) {
      const params = formInfo.value.data
      thirdSystemLoginTestConnection(params).then(res => {
        console.log(res)
      })
    }
  })
}
onMounted(() => {
  getList()
  initRules()
})
</script>

<style scoped></style>
